//
//  BLECenter.h
//  BLESDKDemo
//
//  Created by 李阳 on 2018/4/5.
//  Copyright © 2018年 李阳. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>

@interface BLEUUIDItem : NSObject

/// 服务名
@property (nonatomic, copy) NSString *serviceName;
/// 通知特征名
@property (nonatomic, copy) NSString *notifyCharacteristicName;
/// 写特征名
@property (nonatomic, copy) NSString *writeCharacteristicName;

- (NSArray<CBUUID *> *)characteristicUUIDs;

+ (instancetype)itemWithServiceName:(NSString *)serviceName
                         notifyName:(NSString *)notifyName
                          writeName:(NSString *)writeName;

- (instancetype)initWithServiceName:(NSString *)serviceName
                         notifyName:(NSString *)notifyName
                          writeName:(NSString *)writeName;

@end

/// 蓝牙中心配置
@interface BLECenterConfig : NSObject

/// 主服务
@property (nonatomic, strong) BLEUUIDItem *main;
/// NFC
@property (nonatomic, strong) BLEUUIDItem *NFC;
/// 媒体
@property (nonatomic, strong) BLEUUIDItem *media;
/// 心率
@property (nonatomic, strong) BLEUUIDItem *heart;
/// 支付
@property (nonatomic, strong) BLEUUIDItem *pay;
/// OTA
@property (nonatomic, strong) BLEUUIDItem *OTA;
/// L42 OTA
@property (nonatomic, strong) BLEUUIDItem *OTAL42;

/**
 需要自动连接的设备名称
 @warning `connectAutomatically`为yes才有效
 */
@property (nonatomic, copy) NSString *deviceNameNeedConnectAutomatically;

/// 当蓝牙关闭进行任务时显示alert YES by default
@property (nonatomic, assign) BOOL popAlertWhenPowerOff;


/*!
 *  @property 自动连接
 *
 *  @discussion 自动连接`deviceNameNeedConnectAutomatically`或上一次连接成功的设备 默认位YES
 *
 */
@property (nonatomic, assign) BOOL connectAutomatically;


/// 一次最大的写入数据长度 20 by default
@property (nonatomic, assign) NSUInteger maxWriteValueLengthForOnce;

/**
 The dispatch queue for `block`. If `NULL` (default), the main queue is used.
 */
@property (nonatomic, strong, nullable) dispatch_queue_t completionQueue;

/**
 The dispatch group for `completionBlock`. If `NULL` (default), a private dispatch group is used.
 */
@property (nonatomic, strong, nullable) dispatch_group_t completionGroup;

+ (instancetype)defaultConfig;

@end


/// 蓝牙中心状态
typedef NS_ENUM(NSUInteger, BLECenterState)
{
    BLECenterStateUnknown = 0, ///< 未知状态
    BLECenterStateResetting, ///< 正在重置
    BLECenterStateDisable, ///< 不可用
    BLECenterStateAvailable, ///< 可用
    BLECenterStateSearching, ///< 正在搜索
};


#pragma mark -

@class BLEDevice;
@class BLERequest, BLETask;

/// 蓝牙中心
@interface BLECenter : NSObject

+ (instancetype)center;

- (instancetype)initWithConfig:(BLECenterConfig *)config NS_DESIGNATED_INITIALIZER;

/// 打开日志
- (void)setLogEnable:(BOOL)enable;

/// 控制所有蓝牙交互 default is `YES`
@property (nonatomic, assign, getter=isEnable) BOOL enable;

/// 状态
@property (nonatomic, assign, readonly) BLECenterState state;
/// 上一次连接的设备名
@property (nonatomic, copy, readonly) NSString *lastConnectedDeviceName;
/// 已连接的设备名
@property (nonatomic, strong, readonly) BLEDevice *connectedDevice;
/// 蓝牙中心
@property (nonatomic, strong, readonly) CBCentralManager *manager;
/// 蓝牙中心配置
@property (nonatomic, strong, readonly) BLECenterConfig *config;

#pragma mark Search
- (void)searchDevicesWithTimeout:(NSTimeInterval)timeout
                     filterBlock:(BOOL(^)(NSString *name, NSNumber *rssi))filterBlock
                            progress:(void (^)(BLEDevice *device, NSTimeInterval use, BOOL *stop))progressBlock
                          completion:(void(^)(NSTimeInterval use, NSError *error))completion;

- (void)searchDevicesWithServices:(NSArray<NSString *>*)UUIDStrings
                          options:(NSDictionary<NSString *, id> *)options
                          timeout:(NSTimeInterval)timeout
                      filterBlock:(BOOL(^)(NSString *name, NSNumber *rssi))filterBlock
                         progress:(void (^)(BLEDevice *device, NSTimeInterval use, BOOL *stop))progressBlock
                       completion:(void(^)(NSTimeInterval use, NSError *error))completion;

- (void)stopSearch;


#pragma mark Connect
- (void)connectToDevice:(BLEDevice *)device
                timeout:(NSTimeInterval)timeout
                options:(NSDictionary<NSString *, id> *)options
             completion:(void(^)(NSTimeInterval use, NSError *error))completion;

- (void)connectToDeviceNamed:(NSString *)name
                     timeout:(NSTimeInterval)timeout
                     options:(NSDictionary<NSString *, id> *)options
                  completion:(void(^)(NSTimeInterval use, NSError *error))completion;

- (void)connectAutomaticallyWithTimeout:(NSTimeInterval)timeout
                                options:(NSDictionary<NSString *, id> *)options
                             completion:(void(^)(NSTimeInterval use, NSError *error))completion;

- (void)cancelConnectionWithTimeout:(NSTimeInterval)timeout
                         completion:(void(^)(NSTimeInterval use, NSError *error))completion;

#pragma mark Request
- (__kindof BLETask *)sentRequest:(BLERequest *)request
                          success:(void (^)(BLERequest *request))success
                          failure:(void (^)(NSError *error))failure;


@end






